package com.sunwayworld.basemodule.common.utils;

import com.sunwayworld.basemodule.common.constant.LimsConstant;
import com.sunwayworld.framework.utils.ObjectUtils;
import com.sunwayworld.framework.utils.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.xssf.usermodel.XSSFCell;

import java.text.DecimalFormat;

public class LimsUtils {

    public static final String getStringCellValue(XSSFCell cell) {
        Object cellValue = "";
        label28:
        switch (cell.getCellType()) {
            case STRING:
                cellValue = StringUtils.trim(cell.getRichStringCellValue().getString());
                break;
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    cellValue = cell.getDateCellValue();
                } else {
                    cellValue = cell.getNumericCellValue();
                }
                break;
            case BOOLEAN:
                cellValue = ObjectUtils.toString(cell.getBooleanCellValue());
                break;
            case FORMULA:
                FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
                CellValue cv = evaluator.evaluate(cell);
                switch (cv.getCellType()) {
                    case STRING:
                        cellValue = StringUtils.trim(cv.getStringValue());
                        break label28;
                    case NUMERIC:
                        cellValue = "" + cv.getNumberValue();
                        break label28;
                    case BOOLEAN:
                        cellValue = ObjectUtils.toString(cv.getBooleanValue());
                        break label28;
                    default:
                        cellValue = null;
                        break label28;
                }
            default:
                cellValue = "";
        }
        return cellValue != null ? cellValue.toString() : null;
    }

    public static String getHSSStringCellValue(HSSFCell cell) {
        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        String cellType = cell.getCellType().toString();
        String result = "";
        // 数字 ，double
        if (LimsConstant.CELL_TYPE_NUMERIC.equals(cellType)) {
            Double value = cell.getNumericCellValue();
            result = decimalFormat.format(value);
        }
        // 字符
        else if (LimsConstant.CELL_TYPE_STRING.equals(cellType)) {
            result = cell.getStringCellValue();
        }
        return result;
    }

    public static final String getCellValue(Cell cell) {
        DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
        String cellType = cell.getCellType().toString();
        String result = "";
        // 数字 ，double
        if (LimsConstant.CELL_TYPE_NUMERIC.equals(cellType)) {
            Double value = cell.getNumericCellValue();
            result = decimalFormat.format(value);
        }
        // 字符
        else if (LimsConstant.CELL_TYPE_STRING.equals(cellType)) {
            result = cell.getStringCellValue();
        }
        return result;
    }
}
